package cn.kinoko.service.manga.impl;

import cn.kinoko.common.base.model.BaseServiceImpl;
import cn.kinoko.common.code.ErrorCodeEnum;
import cn.kinoko.common.utils.ExceptionUtil;
import cn.kinoko.mapper.manga.MangaMarkCommentMapper;
import cn.kinoko.model.manga.entity.MangaMark;
import cn.kinoko.model.manga.entity.MangaMarkComment;
import cn.kinoko.model.manga.vo.MarkCommentVO;
import cn.kinoko.service.common.util.AuthUtil;
import cn.kinoko.service.manga.MangaMarkCommentService;
import cn.kinoko.service.manga.MangaMarkService;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author kinoko
 */
@Service
public class MangaMarkCommentServiceImpl extends BaseServiceImpl<MangaMarkCommentMapper, MangaMarkComment> implements MangaMarkCommentService {

    @Lazy
    @Resource
    private MangaMarkService mangaMarkService;

    /**
     * 撤销留言
     * @param id 留言id
     * @return 执行结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean undoComment(Long id) {
        Long uid = AuthUtil.getUidAndCheck();
        MangaMarkComment comment = getById(id);
        ExceptionUtil.isNull(comment, ErrorCodeEnum.SYSTEM_ERROR_B0408);
        // 查询标注下是否还有留言
        boolean havOtherComment = lambdaQuery().eq(MangaMarkComment::getMarkId, comment.getMarkId()).ne(MangaMarkComment::getId, id).exists();
        if (!havOtherComment) {
            mangaMarkService.lambdaUpdate().eq(MangaMark::getMarkId, comment.getMarkId()).set(MangaMark::getNotice, false).update();
        }
        // 检查是否为标注人
        ExceptionUtil.assertion(!Objects.equals(uid, comment.getCreateBy()), ErrorCodeEnum.USER_ERROR_A0300);
        return removeById(id);
    }

    /**
     * 查询留言
     * @param markId 留言id
     * @return 留言列表
     */
    @Override
    public List<MarkCommentVO> queryComment(Long markId) {
        Long uid = AuthUtil.getUidAndCheck();
        List<MarkCommentVO> markCommentVOS = getBaseMapper().queryComment(markId);
        for (MarkCommentVO markCommentVO : markCommentVOS) {
            markCommentVO.setOwner(Objects.equals(uid, markCommentVO.getCreateBy()));
        }
        return markCommentVOS;
    }

    /**
     * 删除留言
     * @param id 业务id
     */
    @Override
    public void delById(SFunction<MangaMarkComment, ?> column, Long id) {
        lambdaUpdate().eq(column, id).remove();
    }
    /**
     * 删除留言
     * @param markIds 标注id列表
     */
    @Override
    public void delByMarkIds(List<Long> markIds) {
        lambdaUpdate().in(MangaMarkComment::getMarkId, markIds).remove();
    }

    /**
     * 查询留言
     *
     * @param epRid 生肉章节id
     * @return 留言列表
     */
    @Override
    public List<MarkCommentVO> queryCommentByEpRid(Long epRid) {
        return getBaseMapper().queryCommentByEpRid(epRid);
    }

    /**
     * 查询留言
     *
     * @param epId 章节id
     * @return 留言列表
     */
    @Override
    public List<MarkCommentVO> queryCommentByEpId(Long epId) {
        return getBaseMapper().queryCommentByEpid(epId);
    }
}